package main

import (
	"context"
	"fmt"
	"github.com/qiniu/api.v7/auth/qbox"
	"github.com/qiniu/api.v7/storage"
	"bytes"
	"io/ioutil"
)

func main() {
	accessKey := "TE_-Yqi7633MjIo3haNx4uF-WJaLqatk1SjduHcY"
	secretKey := "tygGSrdMLIpgTie26eqYH41aKd9Rd3S6CRac92Uz"

	bucket:="vvcdn"
	// 这里的默认过期时间是 1h
	putPolicy := storage.PutPolicy{
		Scope: bucket,
	}
	mac := qbox.NewMac(accessKey, secretKey)
	upToken := putPolicy.UploadToken(mac)

	fmt.Println(upToken)

	cfg := storage.Config{}
	// 空间对应的机房
	cfg.Zone = &storage.ZoneHuadong
	// 是否使用https域名
	cfg.UseHTTPS = false
	// 上传是否使用CDN上传加速
	cfg.UseCdnDomains = false
	formUploader := storage.NewFormUploader(&cfg)
	ret := storage.PutRet{}
	putExtra := storage.PutExtra{
		Params: map[string]string{
			"x:name": "github logo",
		},
	}

	fileContent, _ := ioutil.ReadFile("./gitlab.png")

	dataLen := int64(len(fileContent))
	err := formUploader.Put(context.Background(), &ret, upToken, "1024a.png", bytes.NewReader(fileContent), dataLen, &putExtra)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(ret.Key, ret.Hash)
}
